<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页开关灯</title>
    <style>
        #btn{
            width: 50px;
            height: 20px;
            border: 1px solid black;
            text-align: center;
            cursor: pointer;/*当鼠标移上去,变成手的形状 ,移出去还原*/
        }
    </style>
</head>
<body>
    <div id="btn">开关灯</div>
    <script>
        var btn = document.getElementById('btn');
        var bdele = document.body;
        var flag = 0;
        btn.onclick = function(){
            if(flag == 0){
                bdele.style.backgroundColor = 'black';
                this.innerHTML = '开灯';
                this.style.backgroundColor= 'white';
                flag = 1;
            }
            else if(flag == 1){
                bdele.style.backgroundColor ='white';
                this.innerHTML = '关灯';
                this.style.backgroundColor = 'rgb(117, 117, 111)';
                flag = 0;
            }
        }
    </script>
</body>
</html>